#!/usr/bin/python

import socket
import struct

s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0xd00d))
s.bind(("sn1", 0xd00d))

while 1:
   pac = s.recv(1024)
   print " "
   # tira ethernet
   pac = pac[14:]
   print "Subaddr: %d" % struct.unpack('>H', pac[0:2])
   sampleschunk = struct.unpack('B', pac[2])[0]
   print "Samples Chunk: %d" % sampleschunk
   print "Flag: %d" % struct.unpack('B', pac[3])
   print "Counter: %d" % struct.unpack('>H', pac[4:6])
   canais = struct.unpack('>H', pac[6:8])[0]
   print "Number channels: %d" % canais
   tamsinaisbyte = (canais+1)/2
   bitssinais = pac[8:8+tamsinaisbyte]
   print "%02X"*tamsinaisbyte % struct.unpack('%dB' % tamsinaisbyte, bitssinais)
   samples = pac[8+tamsinaisbyte:]
   print ("%02X"*sampleschunk+" ")*canais % struct.unpack('%dB' % len(samples), samples)
